package com.zwz.controller;

import com.zwz.crawler.basic.TungeeEnterpriseInfo;
import com.zwz.crawler.tendering.TungeeWinningStats;
import com.zwz.crawler.tendering.getBigInfo.TungeeBiddingSearch;
import com.zwz.dto.CompanyOperateStausDto;
import com.zwz.entity.CompanyOperationStatus;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.service.CompanyOperationStatusService;
import com.zwz.vo.BidInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/companies")
public class CompanyOperationStatusController {

    @Autowired
    private CompanyOperationStatusService service;

    @PostMapping
//    public ResponseEntity<?> create(@RequestBody CompanyOperationStatus record) {
    public ResponseEntity<?> create( String enterpriseId) {
        try {
            //1.请求相关的聚合pause point

            //1.1请求聚合企业分类
            //主营业务
            //业务关键词
            //公司规模字段


//            EnterpriseInfo b1 = TungeeEnterpriseInfo.startTungeeEnterpriseInfo();

            //1.2 请求聚合对应的招投标信息
            TungeeWinningStats.startTungeeWinningStats();

            List<BidInfo> bean = TungeeBiddingSearch.startTungeeBiddingSearch(enterpriseId);
//
//            Object ret = TungeeWinningStats.startTungeeWinningStats();
////            Object b2 = TungeeWinningStats.startTungeeWinningStats();
            CompanyOperationStatus record = new CompanyOperationStatus();
            for(int i=0;i<bean.size();i++){

                //2.获取对应返回的解析bean

                record=new CompanyOperationStatus();
                record.setBiddingTitle(bean.get(i).getTitile());
            record.setBiddingPublishDate(bean.get(i).getTime());
            record.setBiddingNoticeType(bean.get(i).getType());
//            record.setBiddingNoticeType(bean.get(i).getB);

                record.setCreatedAt(new Date());
                record.setUpdatedAt(new Date());

                service.create(record);
                
                
            }


            CompanyOperateStausDto dto=new CompanyOperateStausDto(null,record);
            dto.setMainInfo(null);
            dto.setCompanyOperateStaus(record);
            return ResponseEntity.status(HttpStatus.CREATED).body(dto);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

//    @GetMapping("/high-tech")
//    public ResponseEntity<List<CompanyOperationStatus>> getHighTech(
//            @RequestParam Boolean isHighTech) {
//        return ResponseEntity.ok(service.getHighTechCompanies(isHighTech));
//    }
//
//    @GetMapping("/financing")
//    public ResponseEntity<List<CompanyOperationStatus>> getByFinancing(
//            @RequestParam String round) {
//        return ResponseEntity.ok(service.getByFinancingRound(round));
//    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(
            @PathVariable Integer id,
            @RequestBody CompanyOperationStatus record) {
        try {
            if (!id.equals(record.getId())) {
                return ResponseEntity.badRequest().body("ID不一致");
            }
            service.update(record);
            return ResponseEntity.ok(record);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}